home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
rpc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
5KB
|
266 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* rpc -
* Put different things to a stream: floats, ints, strings,
* and newlines.
*
* Paul Haeberli - 1985
*/
#include "stdio.h"
#include "ctype.h"
putfloat(file,f)
FILE *file;
float f;
{
int intpart;
int fracpart;
char tempbuf[40];
char *cptr;
int i;
if(f<0.0) {
putc('-',file);
f = -f;
}
intpart = f;
cptr = tempbuf;
do {
*cptr++ = '0'+(intpart % 10);
intpart /= 10;
} while(intpart);
while(cptr != tempbuf) {
cptr--;
putc(*cptr,file);
}
putc('.',file);
fracpart = 1000000*(f-intpart);
if(fracpart != 0) {
cptr = tempbuf;
for(i=0; i<6; i++) {
if((fracpart % 10) != 0)
break;
fracpart /= 10;
}
if(i == 6)
putc('0',file);
else {
for(;i<6; i++) {
*cptr++ = '0'+(fracpart % 10);
fracpart /= 10;
}
while(cptr != tempbuf) {
cptr--;
putc(*cptr,file);
}
}
} else
putc('0',file);
putc(' ',file);
}
putint(file,i)
FILE *file;
int i;
{
char tempbuf[40];
char *cptr;
if(i<0.0) {
putc('-',file);
i = -i;
}
cptr = tempbuf;
do {
*cptr++ = '0'+(i % 10);
i /= 10;
} while(i);
while(cptr != tempbuf) {
cptr--;
putc(*cptr,file);
}
putc(' ',file);
}
putstr(file,str)
FILE *file;
char *str;
{
char tempbuf[40];
char *cptr;
while(*str)
putc(*str++,file);
putc(' ',file);
}
putnewline(file)
FILE *file;
{
putc('\n',file);
}
/*
* get different things from a stream
*
* floats, ints, strings, newlines.
*
*/
float getfloat(file)
FILE *file;
{
int c;
int intpart;
int fracpart;
float retval;
int cnt;
int isneg;
c = getc(file);
while(c != EOF && isspace(c))
c = getc(file);
if(c == '-') {
isneg = 1;
c = getc(file);
} else {
isneg = 0;
if(c == '+')
c = getc(file);
}
intpart = 0;
fracpart = 0;
retval = 0.0;
while(isdigit(c)) {
intpart = 10*intpart+(c-'0');
c = getc(file);
}
if(c == '.') {
c = getc(file);
cnt = 0;
while(isdigit(c)) {
fracpart = 10*fracpart+(c-'0');
c = getc(file);
cnt++;
}
switch(cnt) {
case 0:
retval = 0.0;
break;
case 1:
retval = fracpart/10.0;
break;
case 2:
retval = fracpart/100.0;
break;
case 3:
retval = fracpart/1000.0;
break;
case 4:
retval = fracpart/10000.0;
break;
case 5:
retval = fracpart/100000.0;
break;
case 6:
retval = fracpart/1000000.0;
break;
case 7:
retval = fracpart/10000000.0;
break;
case 8:
retval = fracpart/100000000.0;
break;
case 9:
retval = fracpart/1000000000.0;
break;
case 10:
retval = 0.0;
break;
default:
printf("getfloat: too many digits after decimal point\n");
break;
}
}
if(c == '\n')
ungetc(c,file);
if(isneg)
return -(retval+intpart);
else
return (retval+intpart);
}
getint(file)
FILE *file;
{
int c, val;
int isneg;
c = getc(file);
while(isspace(c))
c = getc(file);
if(c == '-') {
isneg = 1;
c = getc(file);
} else {
isneg = 0;
if(c == '+')
c = getc(file);
}
val = 0;
while (isdigit(c)) {
val = 10*val + (c-'0');
c = getc(file);
}
if(c == '\n')
ungetc(c,file);
if(isneg)
return -val;
else
return val;
}
getstr(file,str)
FILE *file;
char *str;
{
int c;
c = getc(file);
if(c != '\n') {
while(isspace(c))
c = getc(file);
while (c != EOF && !isspace(c)) {
*str++ = c;
c = getc(file);
}
if(c == '\n')
ungetc(c,file);
}
*str = 0;
return (c!=EOF);
}
getnewline(file)
FILE *file;
{
int c;
c = getc(file);
while (c != EOF && c != '\n')
c = getc(file);
}